<?php

class html {
	
	var $fname_prefix = "fields[";
	var $fname_suffix = "]";
	
	function text($name,$default = null,$other_props = null) {
		$out = '<input type="text" name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .' value="'. stripslashes(htmlentities($default)) .'" />'."\n";
		return $out;
	}
	
	function hidden($name,$default = null) {
		$out = '<input type="hidden" name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .' value="'. stripslashes(htmlentities($default)) .'" />'."\n";
		return $out;
	}
	
	function password($name,$other_props = null) {
		$out = '<input type="password" name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .' />'."\n";
		return $out;
	}
	
	function textarea($name,$default = null,$other_props = null) {
		$out = '<textarea name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .'>'. stripslashes(htmlentities($default)) .'</textarea>'."\n";
		return $out;
	}
	
	function select($name,$options_a,$default = null,$other_props = null) {
		$out = '<select name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .'>'."\n";
		
		if (is_array($options_a)) {
			foreach ($options_a as $name => $value) {
				$out .= "\t".'<option value="'. $value .'"'. ($value == $default ? ' selected="selected"' : '') .'>'. $name .'</option>'."\n";
			}
		}
		
		$out .= '</select>'."\n";
		return $out;
	}
	
	function radio($name,$label,$value,$checked = false,$label_before = false,$other_props = null) {
		$label = '<label for="f_'. $name .'_'. $value .'" style="float: none;">'. $label .'</label>';
		$out = '<input type="radio" name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'_'. $value .'" class="noborder" value="'. $value .'"'. ($checked == $value ? ' checked="checked"' : '') . (is_array($other_props) ? $this->_parse_props($other_props) : '') .' />';
		return (!$label_before ? $out.$label : $label.$out);
	}
	
	function checkbox($name,$label,$checked = false,$label_before = false,$other_props = null) {
		$label = '<label for="f_'. $name .'" style="float: none;">'. $label .'</label>';
		$out = '<input type="checkbox" name="'. $this->fname_prefix.$name.$this->fname_suffix .'" id="f_'. $name .'" class="noborder" value="1"'. ($checked ? ' checked="checked"' : '') . (is_array($other_props) ? $this->_parse_props($other_props) : '') .' />';
		return (!$label_before ? $out.$label : $label.$out);
	}
	
	function file($name,$other_props = null) {
		$out = '<input type="file" name="'. $name .'" id="f_'. $name .'"'. (is_array($other_props) ? $this->_parse_props($other_props) : '') .' />'."\n";
		return $out;
	}
	
	function date($name,$default_date = null,$year_offset = 10,$show_time = false,$is_optional = false,$is_checked = false,$monthnames_a = null) {
		if (!$default_date)
			$default_date = date("Y-m-d H:m");
			
		if (!$monthnames_a)
			$monthnames_a = array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
		
		for ($i = 0; $i < 31; $i++)
			$days[] = sprintf("%02d", ($i+1));
		
		for ($i = 0; $i < 12; $i++)
			$months[$monthnames_a[$i]] = sprintf("%02d", ($i+1));
			
		if ($show_time) {
			for ($i = 0; $i < 24; $i++)
				$hours[] = sprintf("%02d", $i);
				
			for ($i = 0; $i < 60; $i++)
				$mins[] = sprintf("%02d", $i);
		}
		
		$curyear = (int)date("Y");
		
		for ($i = 0; $i < (($year_offset * 2) + 1);$i++) {
			if ($i < $year_offset)
				$years[] = $curyear - ($year_offset - $i);
			else if ($i == $year_offset)
				$years[] = $curyear;
			else
				$years[] = $curyear + ($i - $year_offset);
		}
		
		$days = array_combine($days,$days);
		$years = array_combine($years,$years);
		
		if ($show_time) {
			$hours = array_combine($hours,$hours);
			$mins = array_combine($mins,$mins);
			
			$default_hour = substr($default_date,11,2);
			$default_min = substr($default_date,14,2);
		}
		
		$default_day = substr($default_date,8,2);
		$default_month = substr($default_date,5,2);
		$default_year = substr($default_date,0,4);
		
		$select_props = array("onchange" => "updateHiddenDateField('". $name ."');");
		$checkbox_props = array("onclick" => "toggler('". $name ."__container'); updateHiddenDateField('". $name ."');");
		
		$out = ($is_optional ? $this->checkbox($name."__toggler",'',$is_checked,false,$checkbox_props) : "") ."<div id=\"". $name ."__container\" style=\"display: ". ($is_optional ? ($is_checked ? "inline" : "none") : "inline") .";\">". $this->select($name."__d",$days,$default_day,$select_props) . $this->select($name."__m",$months,$default_month,$select_props) . $this->select($name."__y",$years,$default_year,$select_props);
		
		if ($show_time)
			$out .= "/ ". $this->select($name."__hour",$hours,$default_hour,$select_props) .": ". $this->select($name."__min",$mins,$default_min,$select_props);
		
		$out .= "</div>\n". $this->hidden($name,($is_optional ? ($is_checked ? $default_date : "") : $default_date));
		
		return $out;
	}
	
	function _parse_props($array) {
		foreach($array as $attr => $val)
			$out .= ' '. $attr .'="'. $val .'" ';
			
		return $out;
	}
	
	function setFieldNamePrefix($str) {
		$this->fname_prefix = $str;
	}
	
	function setFieldNameSuffix($str) {
		$this->fname_suffix = $str;
	}
	
}

?>
